import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../appwidget/snack_bar_widget.dart';
import 'package:flutter_app/util/AroutUtil.dart';
import 'package:flutter_app/util/DataUtil.dart';

import 'EventBusPage.dart';
import 'GestureDetectorWidget.dart';
import 'StackWidget.dart';
import 'local_notification.dart';

/// 事件总线机制

class EventGrideWidget extends StatefulWidget {
  final String ?title;

  EventGrideWidget({this.title}) : super();

  @override
  State<StatefulWidget> createState() {
    return _OriginalState();
  }
}

class _OriginalState extends State<EventGrideWidget> {
  var list = DataUtil.getEvent();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title??""),
      ),
      body: Center(
        child: GridView.builder(
            padding: const EdgeInsets.all(15),
            itemCount: list.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                //横轴元素个数
                crossAxisCount: 3,
                //纵轴间距
                mainAxisSpacing: 10.0,
                //横轴间距
                crossAxisSpacing: 10.0,
                //子组件宽高长度比例
                childAspectRatio: 1.0),
            itemBuilder: (BuildContext context, int index) {
              return getItemContainer(list[index], index, context);
            }),
      ),
    );
  }
}

Widget getItemContainer(String item, int index, BuildContext context) {
  return Container(
    alignment: Alignment.center,
    width: 200,
    height: 200,
    child: GestureDetector(
      child: DecoratedBox(
        decoration: BoxDecoration(
            gradient: LinearGradient(colors: [Colors.blue, Colors.black]),
            //背景渐变
            borderRadius: BorderRadius.circular(3.0),
            boxShadow: [
              BoxShadow(
                  color: Colors.grey, offset: Offset(2.0, 2.0), blurRadius: 4.0)
            ]),
        child: Center(
          child: Text(
            item,
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 18, color: Colors.white),
          ),
        ),
      ),
      onTap: () {
        toArount(index, context, item);
      },
    ),
  );
}

void toArount(int index, BuildContext context, String title) {
  switch (index) {
    case 0:
      AroutUtil.goTo(context, GestureDetectorWidget(title: title));
      break;
    case 1:
      AroutUtil.goTo(context, StackWidget(title: title));
      break;
    case 2:
      AroutUtil.goTo(context, EventBusPage(title: title));
      break;
    case 3:
      AroutUtil.goTo(context, LocalNotificationPage(title: title));
      break;
    case 4:
      AroutUtil.goTo(context, SnackBarWidget(text: title));
      break;
  }
}
